package com.zsd.android.common

import android.app.Activity
import android.app.Application
import android.os.Bundle
import com.zsd.android.common.utils.MLog
import java.util.*

class BaseApplication: Application(), Application.ActivityLifecycleCallbacks {

    override fun onCreate() {
        super.onCreate()
        MLog.mLog("BaseApplication onCreate")
        registerActivityLifecycleCallbacks(this)
    }

    companion object {
        val activities: LinkedList<Activity> = LinkedList()

        fun killAll() {
            var copy: LinkedList<Activity> = LinkedList(activities)
            for (activity in copy) {
                activity.finish()
            }
            android.os.Process.killProcess(android.os.Process.myPid())
        }
    }

    override fun onActivityCreated(activity: Activity, bundle: Bundle?) {
        synchronized(this) {
            activities.add(activity)
        }
    }

    override fun onActivityPaused(activity: Activity) {
    }

    override fun onActivityStarted(activity: Activity) {
    }

    override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle) {
    }

    override fun onActivityStopped(activity: Activity) {
    }

    override fun onActivityResumed(activity: Activity) {
    }

    override fun onActivityDestroyed(activity: Activity) {
        synchronized(this) {
            activities.remove(activity)
        }
    }
}